package com.ewing.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.axis.utils.StringUtils;
import org.apache.log4j.Logger;

import com.ewing.busi.seller.model.SellerShop;
import com.ewing.busi.seller.service.SellerShopService;
import com.ewing.busi.web.service.WebTemplatePackageService;
import com.ewing.common.constant.SystemProperty;
import com.ewing.core.app.action.base.BaseAction;
import com.ewing.core.app.action.base.ResponseData;
import com.ewing.core.app.action.base.ResponseUtils;
import com.ewing.core.app.constant.IsEff;
import com.ewing.util.CodecUtils;

/**
 * 商铺装修ACTION
 * 
 * @author tanson lam
 * @creation 2015年1月10日
 */
public class ShopDecorateAction extends BaseAction {

	private static Logger logger = Logger.getLogger(MainAction.class);
	private static final String EDIT_FORM = "/admin/shop/decorate.html";
	@Resource
	private SellerShopService sellerShopService;
	@Resource
	private WebTemplatePackageService webTemplatePackageService;

	/**
	 * 查詢列表
	 */
	public void show() {
		try {
			Map<String, Object> dataModel = new HashMap<String, Object>();
			SellerShop sellerShop = sellerShopService
					.findSellerShop(getLoginUserId());
			if (sellerShop == null) {
				renderAdminFail("请创建商店！");
			}
			dataModel.put("shop", sellerShop);
			dataModel.put("shopUrl", SystemProperty.SHOPDOAMIN + "?shopId="
					+ CodecUtils.bigInt(sellerShop.getId()));
			render(EDIT_FORM, dataModel);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 保存导航栏
	 */
	public void save() {
		ResponseData responseData = null;
		try {
			String id = request.getParameter("id");
			SellerShop sellerShop = new SellerShop();
			this.buildPageData(sellerShop);
			SellerShop oldSellerShop = sellerShopService
					.findSellerShop(getLoginUserId());
			if (oldSellerShop != null && !oldSellerShop.getId().equals(id))
				throw new Exception("错误的商铺ID");
			sellerShop.setUserId(getLoginUserId());
			sellerShop.setIseff(IsEff.EFFECTIVE);
			if (!StringUtils.isEmpty(id)) {
				sellerShop.setId(Integer.valueOf(id));
				baseModelService.update(sellerShop);
			} else {
				baseModelService.save(sellerShop);
			}
			responseData = ResponseUtils.success("保存成功！");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			responseData = ResponseUtils.fail("保存失败！");
		}
		this.outResult(responseData);
	}

}
